﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Web;

/// <summary>
/// Summary description for XuLyAnh
/// </summary>
public class XuLyAnh
{
    public static bool createThumbImage(string duongDanOrg,string duongDanThum,int ThumWidth)
    {
        bool kq = true;
        try
        {
            int daiGoc = 1; int caoGoc = 1;
            int daiThucTe = 1, intCaoThucTe = 1;
            using (System.Drawing.Image anhGoc = System.Drawing.Image.FromFile(duongDanOrg))
            {
                daiGoc = anhGoc.Width; caoGoc = anhGoc.Height;
            }
            int tiLe = ThumWidth / daiGoc;
            intCaoThucTe = tiLe * caoGoc;
            ImageResizer.ResizeSettings st = new ImageResizer.ResizeSettings();
            st.MaxWidth = ThumWidth;
            //st.MaxHeight = intCaoThucTe;
            st.Quality = 24;
            st.Mode = ImageResizer.FitMode.Max;
            //ImageResizer.ImageJob i = new ImageResizer.ImageJob(resizer.WorkingDirectory, thuMuc + "/" + tenAnhThumb, new ImageResizer.ResizeSettings("width=750;height=" + intCaoThucTe + ";mode=max"));
            ImageResizer.ImageJob i = new ImageResizer.ImageJob(duongDanOrg, duongDanThum, st);
            //"width=2000;height=2000;format=jpg;mode=max"
            i.CreateParentDirectory = true; //Auto-create the uploads directory.
            i.Build();

        }catch(Exception exx)
        {
            kq = false;
        }
        return kq;
    }
    /// <summary>
    /// scale by percent. Ham nay khong dung duoc voi anh PNG transparent.
    /// </summary>
    /// <param name="duongDanAnh"></param>
    /// <param name="tiLe"></param>
    /// <returns></returns>
    public static System.Drawing.Image resizeImage(string duongDanAnh, int tiLe)
    {
        using (System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(duongDanAnh))
        {
            float nPercent = ((float)tiLe / 100);

            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;

            int destX = 0;
            int destY = 0;
            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);


            Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
            
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;

        }
        
    }
}
